{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "------\n",
    "* 本学习记录人：许智超\n",
    "* 学习时间：2021-11-25\n",
    "* week13(week08)\n",
    "* 参考的文档：\n",
    "> 1. [python官方文档](https://python.org/doc)\n",
    "> 2. [许老师博客](https://cnblogs.com/xuzhichao)\n",
    "\n",
    "\n",
    "\n",
    "------"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. 列表实践之购物车\n",
    "\n",
    "```\n",
    "任务（25分钟）\n",
    "\n",
    "来源Directory 2 课后习题：\n",
    "\n",
    "　　购物车程序项目：\n",
    "\n",
    "　　要求：1、运行程序后，让用户输入支付宝余额，然后打印我们的商品列表给用户。\n",
    "\n",
    "　　   　 2、让用户输入商品编号进行商品的购买。\n",
    "\n",
    "　　  　　3、用户选择商品后，检查用户的余额是否够，若够则直接扣款，不够则提醒用户。\n",
    "\n",
    "　　　　  4、用户可以随时退出购买，推出时打印用户已购买的商品和支付宝余额。\n",
    "```\n",
    "\n",
    "------\n",
    "\n",
    "\n",
    "```\n",
    "架构：\n",
    "\n",
    "用户输入价格？\n",
    "商品列表 = [\n",
    "\t\t\t[商品1名称，价格，编号？],\n",
    "\t\t\t[商品2名称，价格，编号？]\n",
    "\t\t]\n",
    "\n",
    "打印\n",
    "-----------\n",
    "商品1 编号1 价格1\n",
    "商品2 编号2 价格2\n",
    "商品3 编号3 价格3\n",
    "\n",
    "用户输入编号购买\n",
    "\n",
    "判断1 商品是否存在？\n",
    "\n",
    "判断2 用户余额是否够？ 够的话扣钱\n",
    "\n",
    "结束 ： 用户输入 “退出” 退出购买（while）\n",
    "\t\t打印用户的购买信息，打印余额\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入您的账户余额：100\n",
      "-----------商品列表----------\n",
      "商品编号：0 ,商品名称：珍珠奶茶, 商品价格：6\n",
      "商品编号：1 ,商品名称：奶盖四季柠檬茶, 商品价格：8\n",
      "商品编号：2 ,商品名称：杨枝甘露, 商品价格：7\n",
      "商品编号：3 ,商品名称：圣代, 商品价格：6\n",
      "商品编号：4 ,商品名称：多肉葡萄, 商品价格：10\n",
      "请输入你要购买的商品编号（输入‘q’退出购买）：7\n",
      "您输入的商品不存在，请输入正确的商品编码（输入‘q’退出购买）：\n",
      "-----------商品列表----------\n",
      "商品编号：0 ,商品名称：珍珠奶茶, 商品价格：6\n",
      "商品编号：1 ,商品名称：奶盖四季柠檬茶, 商品价格：8\n",
      "商品编号：2 ,商品名称：杨枝甘露, 商品价格：7\n",
      "商品编号：3 ,商品名称：圣代, 商品价格：6\n",
      "商品编号：4 ,商品名称：多肉葡萄, 商品价格：10\n",
      "请输入你要购买的商品编号（输入‘q’退出购买）：fhnk\n",
      "**您输入的内容不符合要求，请检查重新输入！**\n",
      "-----------商品列表----------\n",
      "商品编号：0 ,商品名称：珍珠奶茶, 商品价格：6\n",
      "商品编号：1 ,商品名称：奶盖四季柠檬茶, 商品价格：8\n",
      "商品编号：2 ,商品名称：杨枝甘露, 商品价格：7\n",
      "商品编号：3 ,商品名称：圣代, 商品价格：6\n",
      "商品编号：4 ,商品名称：多肉葡萄, 商品价格：10\n",
      "请输入你要购买的商品编号（输入‘q’退出购买）：3\n",
      "\n",
      "你点的【圣代】已经购买成功，请等候~，您的余额还有【94】元\n",
      "\n",
      "-----------商品列表----------\n",
      "商品编号：0 ,商品名称：珍珠奶茶, 商品价格：6\n",
      "商品编号：1 ,商品名称：奶盖四季柠檬茶, 商品价格：8\n",
      "商品编号：2 ,商品名称：杨枝甘露, 商品价格：7\n",
      "商品编号：3 ,商品名称：圣代, 商品价格：6\n",
      "商品编号：4 ,商品名称：多肉葡萄, 商品价格：10\n",
      "请输入你要购买的商品编号（输入‘q’退出购买）：2\n",
      "\n",
      "你点的【杨枝甘露】已经购买成功，请等候~，您的余额还有【87】元\n",
      "\n",
      "-----------商品列表----------\n",
      "商品编号：0 ,商品名称：珍珠奶茶, 商品价格：6\n",
      "商品编号：1 ,商品名称：奶盖四季柠檬茶, 商品价格：8\n",
      "商品编号：2 ,商品名称：杨枝甘露, 商品价格：7\n",
      "商品编号：3 ,商品名称：圣代, 商品价格：6\n",
      "商品编号：4 ,商品名称：多肉葡萄, 商品价格：10\n",
      "请输入你要购买的商品编号（输入‘q’退出购买）：2\n",
      "\n",
      "你点的【杨枝甘露】已经购买成功，请等候~，您的余额还有【80】元\n",
      "\n",
      "-----------商品列表----------\n",
      "商品编号：0 ,商品名称：珍珠奶茶, 商品价格：6\n",
      "商品编号：1 ,商品名称：奶盖四季柠檬茶, 商品价格：8\n",
      "商品编号：2 ,商品名称：杨枝甘露, 商品价格：7\n",
      "商品编号：3 ,商品名称：圣代, 商品价格：6\n",
      "商品编号：4 ,商品名称：多肉葡萄, 商品价格：10\n",
      "请输入你要购买的商品编号（输入‘q’退出购买）：1\n",
      "\n",
      "你点的【奶盖四季柠檬茶】已经购买成功，请等候~，您的余额还有【72】元\n",
      "\n",
      "-----------商品列表----------\n",
      "商品编号：0 ,商品名称：珍珠奶茶, 商品价格：6\n",
      "商品编号：1 ,商品名称：奶盖四季柠檬茶, 商品价格：8\n",
      "商品编号：2 ,商品名称：杨枝甘露, 商品价格：7\n",
      "商品编号：3 ,商品名称：圣代, 商品价格：6\n",
      "商品编号：4 ,商品名称：多肉葡萄, 商品价格：10\n",
      "请输入你要购买的商品编号（输入‘q’退出购买）：q\n",
      "-----------购物车----------\n",
      "您的购物车为【[['圣代', 6], ['杨枝甘露', 7], ['杨枝甘露', 7], ['奶盖四季柠檬茶', 8]]】,您的余额还有【72】\n"
     ]
    }
   ],
   "source": [
    "# 商品信息：列表数据\n",
    "# goods_list[int(choice)] 用户输入之后选择的商品信息\n",
    "goods_list = [\n",
    "    ['珍珠奶茶',6],\n",
    "    ['奶盖四季柠檬茶',8],\n",
    "    ['杨枝甘露',7],\n",
    "    ['圣代',6],\n",
    "    ['多肉葡萄',10]\n",
    "]\n",
    "\n",
    "# 用户购物车信息：列表数据\n",
    "user_shopping_cart = []\n",
    "\n",
    "# 用户输入\n",
    "Alipay = int(input('请输入您的账户余额：'))\n",
    "\n",
    "# 打印商品信息\n",
    "# ---------\n",
    "# 商品编号：%s ,商品名称：%s, 商品价格：%s\n",
    "# 商品编号：%s ,商品名称：%s, 商品价格：%s\n",
    "\n",
    "# print(\"商品编号：%s ,商品名称：%s, 商品价格：%s\"%(0,goods_list[0][0],goods_list[0][1]))\n",
    "# print(\"商品编号：%s ,商品名称：%s, 商品价格：%s\"%(1,goods_list[1][0],goods_list[1][1]))\n",
    "# print(\"商品编号：%s ,商品名称：%s, 商品价格：%s\"%(2,goods_list[2][0],goods_list[2][1]))\n",
    "# print(\"商品编号：%s ,商品名称：%s, 商品价格：%s\"%(3,goods_list[3][0],goods_list[3][1]))\n",
    "# print(\"商品编号：%s ,商品名称：%s, 商品价格：%s\"%(4,goods_list[4][0],goods_list[4][1]))\n",
    "while True: # 考虑什么时候退出\n",
    "    print(\"-----------商品列表----------\")\n",
    "    for i in range(len(goods_list)):\n",
    "        print(\"商品编号：%s ,商品名称：%s, 商品价格：%s\"%(i,goods_list[i][0],goods_list[i][1]))\n",
    "\n",
    "    choice = input(\"请输入你要购买的商品编号（输入‘q’退出购买）：\") # 1. 输入编号数字  2. 输入“q”3. 输入的啥也不是！\n",
    "    if choice.isdigit():\n",
    "        if int(choice)>=0 and int(choice)<len(goods_list):\n",
    "        # 一、数字 在 列表范围内\n",
    "            # 1. 输入编号数字 数值应该在 商品列表范围以内 \n",
    "            # 判断1 ： Alipay  VS  goods_list[choice][1]（商品价格）   goods_list[int(choice)] (用户输入之后选择的商品信息)\n",
    "            if Alipay >= goods_list[int(choice)][1]:\n",
    "                # 1. Alipay重新赋值\n",
    "                Alipay = Alipay - goods_list[int(choice)][1] # 扣钱\n",
    "                print(\"\\n你点的【%s】已经购买成功，请等候~，您的余额还有【%s】元\\n\"%(goods_list[int(choice)][0],Alipay))\n",
    "                user_shopping_cart.append(goods_list[int(choice)] ) # 增加用户选择的商品信息\n",
    "            else:\n",
    "                print(\"\\n你的钱包不够啊，换种方式支付吧~\\n\")\n",
    "        else:\n",
    "        # 二、数字 不在 列表范围内\n",
    "            print(\"您输入的商品不存在，请输入正确的商品编码（输入‘q’退出购买）：\")\n",
    "    elif choice == \"q\":\n",
    "        # 2. 输入“q”\n",
    "        print(\"-----------购物车----------\")\n",
    "        print(\"您的购物车为【%s】,您的余额还有【%s】\"%(user_shopping_cart,Alipay))\n",
    "        break    \n",
    "    else:\n",
    "        # 3.  输入的啥也不是！不符合要求\n",
    "        print(\"**您输入的内容不符合要求，请检查重新输入！**\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入你要购买的商品编号：3\n",
      "7\n"
     ]
    }
   ],
   "source": [
    "choice = input(\"请输入你要购买的商品编号：\") # input 默认是字符串\n",
    "print(goods_list[int(choice)][1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(goods_list)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2. 请同学们尝试完成实践（二）\n",
    "\n",
    "```\n",
    "# -*- coding:utf-8 -*-\n",
    "# Author:Zhichao\n",
    "#猜数字（0-100）\n",
    "\n",
    "\n",
    "import getpass\n",
    "hide_card = int(getpass.getpass(\"hide_card:\"))\n",
    "count = 0\n",
    "\n",
    "if hide_card <100 and hide_card >0:\n",
    "    while True:\n",
    "        if count <5:\n",
    "            guess_digit = int(input(\"guess_digit:\"))\n",
    "            if guess_digit == hide_card:\n",
    "                print(\"yes, you got it.\")\n",
    "                break\n",
    "            elif guess_digit > hide_card:\n",
    "                print(\"think smaller...\")\n",
    "            else:\n",
    "                print(\"think bigger...\")\n",
    "            count +=1\n",
    "        else:\n",
    "            print(\"猜这么多次都不对,你个笨蛋!\")\n",
    "            print(\"正确答案：%s\"%(hide_card))\n",
    "            break\n",
    "else:\n",
    "    print(\"请输入数字在0-99之间。不要耍赖！\")\n",
    "\n",
    "\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
